//
//  PreferencesWindowController.m
//  UIElementInspector
//
//  Created by Ben Wells on 11/28/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Preferences.h"
#import "PreferencesWindowController.h"
#import "AppDelegate.h"

@implementation PreferencesWindowController
@synthesize singleElementRadio;
@synthesize wholeApplicationRadio;
@synthesize allNotificationsButton;
@synthesize noNotificationsButton;
@synthesize addNotificationTextField;
@synthesize removeNotificationButton;
@synthesize fontSizeCombo;
@synthesize interactionCheck;
@synthesize notificationsCheck;
@synthesize notificationsTable;

- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
    }
    
    return self;
}

- (NSString*) selectedNotification {
    NSArray *notifications = [[Preferences sharedPreferences] allNotifications];
    int row = [notificationsTable selectedRow];
    return [notifications objectAtIndex:row];
}

- (BOOL) hasSelectedNotification {
    return [notificationsTable selectedRow] >= 0;
}

- (void)enableDisable
{
    BOOL isUnlocked = ![[NSApp delegate] isLocked];
    NSInteger lockNotifications = [[Preferences sharedPreferences] lockNotifications];
    [fontSizeCombo setEnabled:isUnlocked];
    [interactionCheck setEnabled:isUnlocked];
    [notificationsCheck setEnabled:isUnlocked];
    [singleElementRadio setEnabled:(isUnlocked && lockNotifications)];
    [wholeApplicationRadio setEnabled:(isUnlocked && lockNotifications)];
    [notificationsTable setEnabled:isUnlocked];
    [allNotificationsButton setEnabled:isUnlocked];
    [noNotificationsButton setEnabled:isUnlocked];
    [addNotificationTextField setEnabled:isUnlocked];
    BOOL canRemove = [self hasSelectedNotification] && ![[Preferences sharedPreferences] isSystemNotification:[self selectedNotification]];
    [removeNotificationButton setEnabled:isUnlocked && canRemove];
}

- (void)windowDidLoad
{
    [super windowDidLoad];
    [(NSPanel *)[self window] setBecomesKeyOnlyIfNeeded:YES];
    [fontSizeCombo selectItemWithTitle:[[Preferences sharedPreferences] fontSize]];
    NSInteger lockInteraction = [[Preferences sharedPreferences] lockInteraction];
    [interactionCheck setState:lockInteraction];
    NSInteger lockNotifications = [[Preferences sharedPreferences] lockNotifications];
    [notificationsCheck setState:lockNotifications];
    BOOL singleElement = [[Preferences sharedPreferences] notificationsForSingleElement];
    [singleElementRadio setState:singleElement];
    [wholeApplicationRadio setState:!singleElement];
    [self enableDisable];
}

- (IBAction)fontSizeChanged:(id)sender {
    NSString *fontSize = [fontSizeCombo titleOfSelectedItem];
    [[Preferences sharedPreferences] setFontSize:fontSize];
}

- (IBAction)interactionChanged:(id)sender {
    NSInteger lockInteraction = [interactionCheck state];
    [[Preferences sharedPreferences] setLockInteraction:lockInteraction];
}

- (IBAction)notificationsChanged:(id)sender {
    NSInteger lockNotifications = [notificationsCheck state];
    [[Preferences sharedPreferences] setLockNotifications:lockNotifications];
    [self enableDisable];
}

- (IBAction)allNotificationsClicked:(id)sender {
    [[Preferences sharedPreferences] setAllNotificationsEnabled:1];
    [notificationsTable reloadData];
}

- (IBAction)noNotificationsClicked:(id)sender {
    [[Preferences sharedPreferences] setAllNotificationsEnabled:0];
    [notificationsTable reloadData];
}

- (IBAction)wholeApplicationClicked:(id)sender {
    BOOL singleElement = ![wholeApplicationRadio state];
    [[Preferences sharedPreferences] setNotificationsForSingleElement:singleElement];
    [singleElementRadio setState:singleElement];
    [wholeApplicationRadio setState:!singleElement];
}

- (IBAction)singleElementClicked:(id)sender {
    BOOL singleElement = [singleElementRadio state];
    [[Preferences sharedPreferences] setNotificationsForSingleElement:singleElement];
    [singleElementRadio setState:singleElement];
    [wholeApplicationRadio setState:!singleElement];
}

- (IBAction)notificationAdded:(id)sender {
    NSString* newNotification = [addNotificationTextField stringValue];
    if ([[Preferences sharedPreferences] notificationExists:newNotification]) {
        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
        [alert setAlertStyle:NSWarningAlertStyle];
        [alert setMessageText:@"That notification is already in my extensive database."];
        [alert setInformativeText:@"You can't add it again. I would get confused, and may cause irrepairable damage to your computer."];
        [alert addButtonWithTitle:@"OK"];
        [alert runModal];
    } else {
        [[Preferences sharedPreferences] addNotification:newNotification];
        [notificationsTable reloadData];
    }
}

- (IBAction)removeNotificationClicked:(id)sender {
    NSString* notification = [self selectedNotification];
    if ([[Preferences sharedPreferences] isSystemNotification:notification]) {
        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
        [alert setAlertStyle:NSWarningAlertStyle];
        [alert setMessageText:@"Um .... that's a system notification!"];
        [alert setInformativeText:@"Maybe I'm being a control freak, but I won't let you delete that. Why don't you just disable it?"];
        [alert addButtonWithTitle:@"OK"];
        [alert runModal];
    } else {
        [[Preferences sharedPreferences] removeNotification:notification];
        [notificationsTable reloadData];
    }
}

- (IBAction)tableViewClicked:(id)sender {
    [self enableDisable];
}

@end
